﻿public class GameManager {
	// === Public =====================================================================================================
	public static GameManager CreateInstance() {
		var instance = new GameManager();
		return instance;
	}

	public void Start() {
		InitializeLevel();
		InitializeGameControls();
		Managers.GuiPanel.HideAllPanelAndShowPanel(GUIPanelType.Info);
	}

	public void Stop() {
		DestroyGameControls();
		DestroyLevel();
		GameData.GetInstance.ResetGame();
		PlayerStats.GetInstance.ResetStats();
	}

	public void Restart() {
		Stop();
		Start();
	}

	public void Death() {
		PlayerStats.GetInstance.ReduceLife();
		PlayerStats.GetInstance.ResetSpeed();
		DestroyGameControls();
		if (PlayerStats.GetInstance.Life <= 0) {
			DestroyLevel();
			Managers.GuiPanel.HideAllPanelAndShowPanel(GUIPanelType.GameOver);
		} else {
			Managers.GuiPanel.HideAllPanelAndShowPanel(GUIPanelType.LossLife);
		}
	}

	public void Reset() {
		DestroySnakeAndCamera();
		InitializeSnakeAndCamera();
		InitializeGameControls();
		Managers.GuiPanel.HideAllPanelAndShowPanel(GUIPanelType.Info);
	}

	// === Private ====================================================================================================
	private static void InitializeGameControls() {
		Controls.SnakeControl().CreateControl();
		Controls.AppleControl().CreateControl();
		Controls.CameraControl().CreateControl();
		Controls.InputHelper().CreateControl();
	}

	private static void DestroyGameControls() {
		Controls.SnakeControl().DestroyControl();
		Controls.AppleControl().DestroyControl();
		Controls.CameraControl().DestroyControl();
		Controls.InputHelper().DestroyControl();
	}

	private static void InitializeSnakeAndCamera() {
		Managers.Snake.Create();
		Managers.Camera.Create();
	}

	private static void DestroySnakeAndCamera() {
		Managers.Snake.Destroy();
		Managers.Camera.Destroy();
	}

	private static void InitializeLevel() {
		Managers.Level.Create();
		InitializeSnakeAndCamera();
	}

	private static void DestroyLevel() {
		Managers.Level.Destroy();
		DestroySnakeAndCamera();
	}
}
